package com.demo.logistics.utils;

public class DefaultValidator {
	
	private String validationMsg;
	
	public String getValidationMsg() {
		return validationMsg;
	}

	public void setValidationMsg(String validationMsg) {
		this.validationMsg = validationMsg;
	}

	public DefaultValidator(){
		
	}
	
	private boolean checkLength(String item, int min, int max){
		if((item.length()>=min && item.length()<=max) && item.length() > 0){
			return true;
		}
		else{
			return false;
		}
	}
	
	/**
     * This method checks if a String contains only numbers
     */
    public boolean containsOnlyNumbers(String str) {
        
        //It can't contain only numbers if it's null or empty...
        if (str == null || str.length() == 0){
            return false;
        }
        
        for (int i = 0; i < str.length(); i++) {

            //If we find a non-digit character we return false.
            if (!Character.isDigit(str.charAt(i))){
            	setValidationMsg("no other characters allow except numbers");
                return false;
            }
        }
        
        return true;
    }
	
	private boolean checkContainNoIllegalChars(String item, char[] illegalChars){
		if(item.length()>0 && illegalChars.length >0){
			for(int i=0; i<item.length(); i++){
				for (int j = 0; j < illegalChars.length; j++) {
					if (item.charAt(i) == illegalChars[j]) {
						return false;
					}
				}
			}
		}
		return true;
	}
	
	public boolean validateUsername(String item){
		char[] badChars = {' ', '!', '\"', '\'', '\\', '/', '&', ';'};
		if(checkLength(item, 3, 15)){
			if(checkContainNoIllegalChars(item, badChars)){
				return true;
			}
			else{
				setValidationMsg("Username cannot contain space, !, / and other illegal symbols");
				return false;
			}
		}
		else{
			setValidationMsg("Username must be between 3 to 15 characters");
			return false;
		}
	}
	
	public boolean validatePrice(String item){
		boolean res = item.matches("[0-9]+\\.[0-9]+");
		if(res){
			return true;
		}
		return false;
	}
}
